package com.neu.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.neu.dao.IproblemDao;
import com.neu.pojo.Problem;

public class ProblemDao implements IproblemDao {
	//属性
	private File file1 = new File("problem.json");
	private ObjectMapper om = new ObjectMapper();
	//单例模式
	private static IproblemDao problemDao = new ProblemDao();
	private ProblemDao() {}
	public static IproblemDao getInstance() {
		return problemDao;
	}
	
	//添加问题
	@Override
	public boolean add(Problem p) {
		ArrayList<Problem> problems = new ArrayList<>();//问题容器
		try {
			problems = this.om.readValue(file1,new TypeReference<List<Problem>>(){});//反序列化
			for(Problem problem:problems) {//遍历
				if(problem.getId() == p.getId()) {//找到了
					return false;//返回
				}
			}
			problems.add(p);//添加
			this.om.writeValue(file1, problems);//序列化
			return true;//返回
		} catch (MismatchedInputException e) {//文件内无内容
			problems.add(p);//添加
			try {
				this.om.writeValue(file1, problems);//序列化
				return true;//返回
			} catch (IOException e1) {//异常
				e1.printStackTrace();
				return false;//返回
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;//返回
	}
	
	//删除问题
	@Override
	public boolean delet(Problem p) {
		ArrayList<Problem> problems = new ArrayList<>();//问题容器
		
		try {
			problems = om.readValue(file1,new TypeReference<List<Problem>>(){});//反序列化
			for(int i=0;i<problems.size();i++) {//遍历
				if(problems.get(i).getId() == p.getId()) {//找到了
					problems.remove(i);
					this.om.writeValue(file1, problems);//序列化
					return true;//返回
				}
			}
			return true;//返回
		}catch (MismatchedInputException e) {//文件内无内容
			return true;//返回
		}catch (IOException e) {//异常
			e.printStackTrace();
			return false;//返回
		}
	}
	
	//问题覆盖
	@Override
	public boolean cover(Problem p) {
		ArrayList<Problem> problems = new ArrayList<>();//问题容器
		
		try {
			problems = om.readValue(file1,new TypeReference<List<Problem>>(){});//反序列化
			for(int i=0;i<problems.size();i++) {//遍历
				if(problems.get(i).getId() == p.getId()) {//找到了
					problems.get(i).setOptionA(p.getOptionA());
					problems.get(i).setOptionB(p.getOptionB());
					problems.get(i).setOptionC(p.getOptionC());
					problems.get(i).setProblem(p.getProblem());
					problems.get(i).setType(p.getType());
					return true;//返回
				}
			}
			return false;//返回
		}catch (MismatchedInputException e) {//文件内无内容
			return false;//返回
		}catch (IOException e) {//异常
			e.printStackTrace();
			return false;//返回
		}
	}
	
	//搜索问题
	@Override
	public Problem search(Problem p) {
		ArrayList<Problem> problems = new ArrayList<>();//问题容器
		
		try {
			problems = om.readValue(file1,new TypeReference<List<Problem>>(){});//反序列化
			for(Problem problem: problems) {//遍历
				if(p.getId() == problem.getId()) {//找到了
					return problem;//返回
				}
			}
			return null;//返回
		}catch (MismatchedInputException e) {//文件内无内容
			return null;//返回
		}catch (IOException e) {
			e.printStackTrace();
			return null;//返回
		}
	}
	
	@Override
	public int size() {
		ArrayList<Problem> problems = new ArrayList<>();//问题容器
		
		try {
			problems = this.om.readValue(file1,new TypeReference<List<Problem>>(){});//反序列化
			return problems.size();//返回
		} catch (MismatchedInputException e) {//文件内无内容
			return 0;//返回
		} catch (IOException e) {
			e.printStackTrace();
			return 0;//返回
		}
	}
	
	//得到所有问题
	@Override
	public ArrayList<Problem> list(){
		ArrayList<Problem> problems = new ArrayList<>();//问题容器
		
		try {
			problems = this.om.readValue(file1,new TypeReference<List<Problem>>(){});//反序列化
			return problems;//返回
		} catch (MismatchedInputException e) {//文件内无内容
			return problems;//返回
		} catch (IOException e) {
			e.printStackTrace();
			return null;//返回
		}
	}
	
	//测试 加数据
//	public static void main(String[] args) {
//		ProblemDao.getInstance().add(new Problem(1, "1", "A", "1", "1", "1"));
//		ProblemDao.getInstance().add(new Problem(2, "1", "A", "1", "1", "1"));
//		ProblemDao.getInstance().add(new Problem(3, "1", "A", "1", "1", "1"));
//		ProblemDao.getInstance().add(new Problem(4, "1", "B", "1", "1", "1"));
//		ProblemDao.getInstance().add(new Problem(5, "1", "B", "1", "1", "1"));
//		ProblemDao.getInstance().add(new Problem(6, "1", "C", "1", "1", "1"));
//		ProblemDao.getInstance().add(new Problem(7, "1", "C", "1", "1", "1"));
//	}
}
